<?php

namespace app\api\controller\answers;

use app\api\model\answers\Template;
use app\api\model\answers\LvyouTemp;
use app\api\model\answers\AnswersEvent;
use app\api\model\answers\LvyouEvent;
use app\api\model\car\ImgEvent;
use app\api\service\token\LoginToken;
use app\lib\exception\answers\AnswersException;
use app\lib\exception\car\CarException;
use think\Db;
use think\Exception;
use think\facade\Request;

class Lvyou
{

    protected $Service;
    public function __construct()
    {

    }

    public function lvyouImage(){
        $params = Request::post();
        $url = "http://127.0.0.1:6000/handleImage";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));// 文本提交方式，必须声明请求头
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output);
    }

    public function lvyouTemp()
    {
        $params = Request::post();
        $pageSize = $params['pageSize'] ?? 20;
        $list = LvyouTemp::paginate($pageSize);
        foreach ($list as &$li) {
            $li["format"] = json_decode($li["format"]);
        }
        return writeJson(200,$list,'数据列表！');
    }

    public function getLvyouEvent()
    {
        $result = LvyouEvent::with("lvyoutemp")->where('status', 1)->findOrEmpty();
        if ($result->isEmpty()){
            throw new AnswersException([
                'msg' => '无数据',
                'error_code' => 20001
            ]);
        }
        $result->save(["status"=>2], ["id"=>$result['id']]);
        return writeJson(200,$result,'数据列表！');
    }

    public function backFill()
    {
        $params = Request::post();
        DB::startTrans();
        try {
            $info = LvyouEvent::where("id",$params["id"])->where("status", 2)->findOrEmpty();
            if ($info->isEmpty()) {
                throw new AnswersException([
                    'msg' => "该任务不存在",
                    'error_code' => 20001
                ]);
            }
            $info->save($params,["id"=>$info['id']]);
            // 提交事务
            Db::commit();
        }catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new AnswersException([
                'msg' => "回填失败",
                'error_code' => 20003
            ]);
        }
        return writeJson(200,'',"回填成功");



    }

    public function addEvent()
    {
        $params = Request::post();

        $data = [
            "status" => 1,
            "imgs" => json_encode($params["imgs"], JSON_UNESCAPED_SLASHES),
            "temp_id" => $params["temp_id"],
            "title" => json_encode($params["title"], JSON_UNESCAPED_UNICODE)
        ];
        $event = LvyouEvent::create($data);
        return writeJson(200,$event["id"],'新增id！');
    }


    public function checkEvent()
    {
        $params = Request::post();
        $event = LvyouEvent::where("id", $params["id"])->findOrEmpty();
        if ($event->isEmpty()) {
            throw new AnswersException([
                'msg' => '参数错误',
                'error_code' => 20001
            ]);
        }
        $return = [
            "rub_map" => json_decode($event["rub_map"]),
            "status" => $event["status"]
        ];
        return writeJson(200,$return,'完成！');
    }
}
